package com.yuandian.customer.perception.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.api.customer_perception.po.AreaAnalysisParams;
import com.yuandian.api.customer_perception.vo.*;
import com.yuandian.base.service.ITaskDataService;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.mapper.AreaAnalysisMapper;
import com.yuandian.customer.perception.service.IAreaAnalysisService;
import com.yuandian.enums.TableNameEnum;
import com.yuandian.po.MergeDataConfig;
import com.yuandian.po.MergeField;
import com.yuandian.po.MultipleTableInfo;
import com.yuandian.utils.DynamicTableNameUtil;
import com.yuandian.utils.MergeDataUtil;
import com.yuandian.utils.PageDataInfo;
import com.yuandian.utils.StringUtils;
import com.yuandian.vo.TrendDataVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 地市分析Service实现
 *
 * @Author pmx
 * @Date 2021/8/17
 */
@Service("AreaAnalysisService")
public class AreaAnalysisServiceImpl implements IAreaAnalysisService {

    @Autowired
    private AreaAnalysisMapper areaAnalysisMapper;

    @Autowired
    private ITaskDataService taskDataService;


    /**
     * 区域-系统健康度总览
     * @param params 请求参数
     * @return 通用结果集
     */
    @Override
    public R<Object> areaSystemHealth(AreaAnalysisParams params) {

		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> newInfoList = Collections.singletonList(info);

        // 设置表信息集
        params.setMultipleTableInfoList(newInfoList);
        // 执行查询
        List<AreaSystemHealthVo> list = areaAnalysisMapper.systemHealth(params);
        return R.ok(list);
    }

    /**
     * 区域趋势图
     * @param params 请求参数
     * @return 通用结果集
     */
    @Override
    public R<Object> areaTrend(AreaAnalysisParams params) {
        // 1、获取表信息集
        MultipleTableInfo info = DynamicTableNameUtil.generalSingleTableNamesFromTimeRange(
                params.getQueryStartTime(),
                params.getQueryEndTime());
        // 2、校验表是否存在，剔除不存在的表
		List<String> tableNameList = Collections.singletonList(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());

        // 3、设置表名、时间范围矫正
        params.setTableNameList(tableNameList);
        params.setQueryStartTime(info.getStartTime());
        params.setQueryEndTime(info.getEndTime());
        // 4、执行查询
        List<AreaTrendVo> areaTrendVos = areaAnalysisMapper.areaTrend(params);
        // 5、归并数据
        Integer minScale = DynamicTableNameUtil.getMinScaleByTableType(info.getTableType());
        Integer scale = null;
        if (params.getScale() == null) {
            params.setScale(1800);
        }
        scale = DynamicTableNameUtil.getScaleByMinScaleAndParam(minScale, params.getScale());
        List<MergeField> mergeFieldList = new ArrayList<>();
        mergeFieldList.add(new MergeField("visitCount"));
        mergeFieldList.add(new MergeField("completeCount"));
        mergeFieldList.add(new MergeField("failCount"));
        mergeFieldList.add(new MergeField("alarmCount"));
        Map<String, Object> map = MergeDataUtil.mergeData(new MergeDataConfig.Builder(
                AreaTrendVo.class,
                areaTrendVos,
                params.getQueryStartTime(),
                params.getQueryEndTime(),
                mergeFieldList)
                .labelTimeFormatter("MM-dd'\n'HH:mm")
                .scale(scale)
                .build());
        map.put("minScale", minScale);
        // 6、重新构建趋势图结果对象
        TrendDataVo trendDataVo = taskDataService.getTrendObject(
                "KHGZ-详单分钟统计",
                params.getQueryStartTime(),
                params.getQueryEndTime(),
                map);
        return R.ok(trendDataVo);
    }

    /**
     * 办事处总览
     * @param params 请求参数
     * @return 通用结果集
     */
    @Override
    public R<Object> businessHallView(AreaAnalysisParams params) {

		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> newInfoList = Collections.singletonList(info);

		// 设置表信息集
		params.setMultipleTableInfoList(newInfoList);

        // 设置表信息集、排序
        params.setMultipleTableInfoList(newInfoList);
        if (StringUtils.isEmpty(params.getOrderByColumn())) {
            params.setOrderByColumn("health_rate");
        } else {
            params.setOrderByColumn(StringUtils.humpToLine(params.getOrderByColumn()));
        }
        if(StringUtils.isEmpty(params.getIsAsc())) {
            params.setIsAsc("asc");
        }
        // 设置分页对象，如果不传pageNum或pageSize，相当于不分页
        Page<BusinessHallViewVo> page = null;
        if (params.getPageNum() > 0 && params.getPageSize() > 0) {
            page = new Page<>(params.getPageNum(), params.getPageSize());
        }
        // 执行查询
        List<BusinessHallViewVo> list = areaAnalysisMapper.businessHallView(page, params);
        // 转换业务数据 返回分页对象
        if (page == null) {
            return R.ok(new PageDataInfo(list.size(), list));
        }
        return R.ok(new PageDataInfo(page, list));
    }

    /**
     * 办事处-系统总览
     * @param params 请求参数
     * @return 通用结果集
     */
    @Override
    public R<Object> systemView(AreaAnalysisParams params) {

		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> newInfoList = Collections.singletonList(info);

		// 3、设置表信息集、排序
        params.setMultipleTableInfoList(newInfoList);
        if (StringUtils.isEmpty(params.getOrderByColumn())) {
            params.setOrderByColumn("health_rate, success_rate, visit_count, complete_count");
        } else {
            params.setOrderByColumn(StringUtils.humpToLine(params.getOrderByColumn()));
        }
        if (StringUtils.isEmpty(params.getIsAsc())) {
            params.setIsAsc("asc");
        }
        // 4、设置分页对象，如果不传pageNum或pageSize，相当于不分页
        Page<SystemViewVo> page = null;
        if (params.getPageNum() > 0 && params.getPageSize() > 0) {
            page = new Page<>(params.getPageNum(), params.getPageSize());
        }
        // 5、执行查询
        List<SystemViewVo> list = areaAnalysisMapper.systemView(page, params);
        // 6、转换业务数据
        // 7、返回分页对象
        if (page == null) {
            return R.ok(new PageDataInfo(list.size(), list));
        }
        return R.ok(new PageDataInfo(page, list));
    }

    /**
     * 办事处-系统-业务总览
     * @param params 请求参数
     * @return 通用结果集
     */
    @Override
    public R<Object> businessView(AreaAnalysisParams params) {

		// 获取表信息集
		MultipleTableInfo info = new MultipleTableInfo();
		info.setTableName(TableNameEnum.KHGZ_BUSI_SYS_DETAIL_STAT.getName());
		List<MultipleTableInfo> newInfoList = Collections.singletonList(info);


		// 3、设置表信息集、排序
        params.setMultipleTableInfoList(newInfoList);
        if (StringUtils.isEmpty(params.getOrderByColumn())) {
            params.setOrderByColumn("health_rate, success_rate, visit_count, complete_count");
        } else {
            params.setOrderByColumn(StringUtils.humpToLine(params.getOrderByColumn()));
        }
        if (StringUtils.isEmpty(params.getIsAsc())) {
            params.setIsAsc("asc");
        }
        // 4、设置分页对象，如果不传pageNum或pageSize，相当于不分页
        Page<BusinessViewVo> page = null;
        if (params.getPageNum() > 0 && params.getPageSize() > 0) {
            page = new Page<>(params.getPageNum(), params.getPageSize());
        }
        // 5、执行查询
        List<BusinessViewVo> list = areaAnalysisMapper.businessView(page, params);
        // 6、转换业务数据
        // 7、返回分页对象
        if (page == null) {
            return R.ok(new PageDataInfo(list.size(), list));
        }
        return R.ok(new PageDataInfo(page, list));
    }
}
